const menu = require("../model/menu");

const fn_init = async () => {
  const menuFi = await menu.findOne();
  if (menuFi === null) {
    await menu.create({});
  }
};
fn_init();

/**
 * @api {get} /menu 菜单列表查询
 * @apiName 菜单列表查询
 * @apiGroup 菜单
 * @apiVersion  1.0.0
 * 
 * @apiSuccess (200) {Array} menuData 菜单数组数据
 * 
 * @apiSuccessExample {type} 返回示例:
 {
    "data": {
        "menuData": [
            {
                "name": "welcome",
                "defaultValue": "welcome",
                "path": "/welcome",
                "key": "0",
                "disableCheckbox": true,
                "icon": "smile",
                "children": null
            },
            {
                "name": "menuManager",
                "defaultValue": "menuManager",
                "path": "/menuManager",
                "key": "1",
                "authority": [
                    "admin",
                    "测试124"
                ],
                "icon": "menu",
                "children": null
            },
            {
                "name": "manager",
                "path": "/manager",
                "key": "qBB2bANuNxmxSd8QS-0-h",
                "authority": [
                    "admin"
                ],
                "icon": "crown",
                "children": [
                    {
                        "name": "userManager",
                        "path": "/manager/userManager",
                        "key": "JOjWw7GPBFUas3asoF4yW",
                        "authority": [
                            "admin"
                        ],
                        "icon": "userManager",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "userList",
                                "path": "/manager/userManager/find",
                                "key": "jO0_MAx6Sszd3NNO5U4vH",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            },
                            {
                                "name": "manageUser",
                                "path": "/manager/userManager/manage",
                                "key": "3P7s9z1NgDHplYBWj6C5r",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            }
                        ]
                    },
                    {
                        "name": "roleManager",
                        "path": "/manager/roleManager",
                        "key": "M0MeRZzt2ufvwaPLUql1K",
                        "authority": [
                            "admin"
                        ],
                        "icon": "crown",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "roleList",
                                "path": "/manager/roleManager/find",
                                "key": "9p7DJAfERQydE1CLfW8mP",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            },
                            {
                                "name": "roleCreate",
                                "path": "/manager/roleManager/create",
                                "key": "NNanzoLUOtZyBK45G4PYY",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            }
                        ]
                    }
                ]
            }
        ],
        "_id": "60de811de14c123cb9038579",
        "registerTime": "2021-07-02T02:59:41.374Z",
        "__v": 0
    },
    "message": "获取菜单成功",
    "code": 0
}
 */
const fn_menu = async (ctx, next) => {
  try {
    const findMenu = await menu.findOne();
    console.log(findMenu, "findMenu");
    if (findMenu) {
      ctx.body = {
        data: findMenu, // findMenu
        message: "获取菜单成功",
        code: 0,
      };
    } else {
      ctx.body = {
        message: "获取菜单失败",
        code: -1,
      };
    }
  } catch (error) {
    ctx.body = {
      message: "菜单查询失败：" + error,
      code: -1,
    };
  }
};

/**
 * @api {post} /createMenu 创建菜单
 * @apiName 创建菜单
 * @apiGroup 菜单
 * @apiVersion  1.0.0
 * 
 * @apiParam {Array} menuData 菜单数组
 * 
 * @apiSuccess (200) {string} type 
 * @apiSuccess (200) {string} currentAuthority 当前权限角色
 * @apiSuccess (200) {string} user 用户名
 * @apiSuccess (200) {string} token token
 * 
 * @apiParamExample  {type} 请求示例:
  {
    menuData : [
            {
                "name": "welcome",
                "defaultValue": "welcome",
                "path": "/welcome",
                "key": "0",
                "disableCheckbox": true,
                "icon": "smile",
                "children": null
            },
            {
                "name": "menuManager",
                "defaultValue": "menuManager",
                "path": "/menuManager",
                "key": "1",
                "authority": [
                    "admin",
                    "测试124"
                ],
                "icon": "menu",
                "children": null
            },
            {
                "name": "manager",
                "path": "/manager",
                "key": "qBB2bANuNxmxSd8QS-0-h",
                "authority": [
                    "admin"
                ],
                "icon": "crown",
                "children": [
                    {
                        "name": "userManager",
                        "path": "/manager/userManager",
                        "key": "JOjWw7GPBFUas3asoF4yW",
                        "authority": [
                            "admin"
                        ],
                        "icon": "userManager",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "userList",
                                "path": "/manager/userManager/find",
                                "key": "jO0_MAx6Sszd3NNO5U4vH",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            },
                            {
                                "name": "manageUser",
                                "path": "/manager/userManager/manage",
                                "key": "3P7s9z1NgDHplYBWj6C5r",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            }
                        ]
                    },
                    {
                        "name": "roleManager",
                        "path": "/manager/roleManager",
                        "key": "M0MeRZzt2ufvwaPLUql1K",
                        "authority": [
                            "admin"
                        ],
                        "icon": "crown",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "roleList",
                                "path": "/manager/roleManager/find",
                                "key": "9p7DJAfERQydE1CLfW8mP",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            },
                            {
                                "name": "roleCreate",
                                "path": "/manager/roleManager/create",
                                "key": "NNanzoLUOtZyBK45G4PYY",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            }
                        ]
                    }
                ]
  }
 * 
 * @apiSuccessExample {type} 返回示例:
 {
    "data": {
        "menuData": [
            {
                "name": "welcome",
                "defaultValue": "welcome",
                "path": "/welcome",
                "key": "0",
                "disableCheckbox": true,
                "icon": "smile",
                "children": null
            },
            {
                "name": "menuManager",
                "defaultValue": "menuManager",
                "path": "/menuManager",
                "key": "1",
                "authority": [
                    "admin",
                    "测试124"
                ],
                "icon": "menu",
                "children": null
            },
            {
                "name": "manager",
                "path": "/manager",
                "key": "qBB2bANuNxmxSd8QS-0-h",
                "authority": [
                    "admin"
                ],
                "icon": "crown",
                "children": [
                    {
                        "name": "userManager",
                        "path": "/manager/userManager",
                        "key": "JOjWw7GPBFUas3asoF4yW",
                        "authority": [
                            "admin"
                        ],
                        "icon": "userManager",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "userList",
                                "path": "/manager/userManager/find",
                                "key": "jO0_MAx6Sszd3NNO5U4vH",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            },
                            {
                                "name": "manageUser",
                                "path": "/manager/userManager/manage",
                                "key": "3P7s9z1NgDHplYBWj6C5r",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "JOjWw7GPBFUas3asoF4yW",
                                "children": null
                            }
                        ]
                    },
                    {
                        "name": "roleManager",
                        "path": "/manager/roleManager",
                        "key": "M0MeRZzt2ufvwaPLUql1K",
                        "authority": [
                            "admin"
                        ],
                        "icon": "crown",
                        "parentKey": "qBB2bANuNxmxSd8QS-0-h",
                        "children": [
                            {
                                "name": "roleList",
                                "path": "/manager/roleManager/find",
                                "key": "9p7DJAfERQydE1CLfW8mP",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "list",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            },
                            {
                                "name": "roleCreate",
                                "path": "/manager/roleManager/create",
                                "key": "NNanzoLUOtZyBK45G4PYY",
                                "authority": [
                                    "admin"
                                ],
                                "icon": "user",
                                "parentKey": "M0MeRZzt2ufvwaPLUql1K",
                                "children": null
                            }
                        ]
                    }
                ]
            }
        ],
        "_id": "60de811de14c123cb9038579",
        "registerTime": "2021-07-02T02:59:41.374Z",
        "__v": 0
    },
    "message": "菜单更新成功",
    "code": 0
}
 */
const create_menu = async (ctx, next) => {
  try {
    const { menuData } = ctx.request.body;
    if (menuData) {
      const findUpdateMenu = await menu.findOneAndUpdate(
        {},
        { menuData },
        { new: true }
      );
      console.log(findUpdateMenu);
      if (findUpdateMenu) {
        ctx.body = {
          data: findUpdateMenu,
          message: "菜单更新成功",
          code: 0,
        };
      } else {
        ctx.body = {
          data: null,
          message: "菜单更新失败",
          code: -1,
        };
      }
    } else {
      ctx.body = {
        data: null,
        message: "菜单不能未空",
        code: -1,
      };
    }
  } catch (error) {
    ctx.body = {
      message: "菜单更新失败：" + error,
      code: -1,
    };
  }
};

module.exports = {
  "GET /menu": fn_menu,
  "POST /createMenu": create_menu,
};
